package com.seven.tools.mybatilsx;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.NonNull;
import org.springframework.core.ResolvableType;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ControllerApiUtils {

    /**
     * @deprecated
     */
    @Deprecated
    public static <T extends IController> void createYapiJson(Class<T> tClass) {
        String controllerPath = "";

        Annotation[] annotations = tClass.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation instanceof org.springframework.web.bind.annotation.RequestMapping) {
                //获取请求路径
                System.out.println(Arrays.toString(((RequestMapping) annotation).value()));
                controllerPath = ((RequestMapping) annotation).value()[0];
            }
        }
        //获取对应实体的字段信息
        ResolvableType resolvableType = ResolvableType.forClass(tClass);
        ResolvableType superType = resolvableType.getSuperType();
        ResolvableType generic = superType.getGeneric(0);
        Class<?> resolve = generic.resolve();
        //获取数据库相关字段信息
        assert resolve != null;
        List<Field> dbFieldList = getDbFieldList(resolve);
        ArrayList<ReqQuery> reqQueries = new ArrayList<>();
        dbFieldList.forEach(field -> {
            ReqQuery reqQuery = new ReqQuery();
            reqQuery.setName(field.getName());
            reqQueries.add(reqQuery);
        });
        String req_query = JSONUtil.toJsonStr(reqQueries);
        //获取api模板文件
        String replace = apiModel
                .replace("{{controllerPath}}", controllerPath)
                .replace("{{req_query}}", req_query)
                .replace("{{name}}", tClass.getSimpleName())
                .replace("{{desc}}", tClass.getSimpleName());
        //输出api
        String path = "F:\\my project\\project\\prometheus-monitor-service\\log";
        File file = FileUtil.writeString(replace, path + "\\" + tClass.getName() + ".json", "UTF-8");
        System.out.println("输出路径" + file.getAbsolutePath());
    }

    @Data
    static class ReqQuery {
        //0非必须 1必须
        private String required = "0";
        //        private String _id = IdUtil.simpleUUID();
        private String name = "";
        private String example = "";
        private String desc = "";
    }

    public static List<Field> getDbFieldList(@NonNull Class<?> dbModel) {
        ArrayList<Field> fields = new ArrayList<>();
        Field[] declaredFields = dbModel.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            if (!Modifier.isStatic(declaredField.getModifiers())) {
                TableField annotation = declaredField.getAnnotation(TableField.class);
                if (annotation != null) {
                    if (annotation.exist()) {
                        fields.add(declaredField);
                    }
                } else {
                    fields.add(declaredField);
                }
            }
        }
        return fields;
    }


    private static final String apiModel = "[\n" +
            "  {\n" +
            "    \"index\": 0,\n" +
            "    \"name\": \"{{name}}\",\n" +
            "    \"desc\": \"{{desc}}\",\n" +
            "    \"add_time\": 1605254675,\n" +
            "    \"up_time\": 1605254675,\n" +
            "    \"list\": [\n" +
            "      {\n" +
            "        \"query_path\": {\n" +
            "          \"path\": \"{{controllerPath}}/search\",\n" +
            "          \"params\": []\n" +
            "        },\n" +
            "        \"edit_uid\": 0,\n" +
            "        \"status\": \"undone\",\n" +
            "        \"type\": \"static\",\n" +
            "        \"req_body_is_json_schema\": true,\n" +
            "        \"res_body_is_json_schema\": true,\n" +
            "        \"api_opened\": false,\n" +
            "        \"index\": 0,\n" +
            "        \"tag\": [],\n" +
            "        \"_id\": 930,\n" +
            "        \"method\": \"GET\",\n" +
            "        \"catid\": 238,\n" +
            "        \"title\": \"模糊查询\",\n" +
            "        \"path\": \"{{controllerPath}}/search\",\n" +
            "        \"project_id\": 48,\n" +
            "        \"req_params\": [],\n" +
            "        \"res_body_type\": \"json\",\n" +
            "        \"req_query\": {{req_query}},\n" +
            "        \"req_headers\": [],\n" +
            "        \"req_body_form\": [],\n" +
            "        \"desc\": \"<p>无参数则查询所有</p>\\n\",\n" +
            "        \"markdown\": \"无参数则查询所有\",\n" +
            "        \"res_body\": \"\",\n" +
            "        \"uid\": 15,\n" +
            "        \"add_time\": 1605255525,\n" +
            "        \"up_time\": 1605255544,\n" +
            "        \"__v\": 0\n" +
            "      },\n" +
            "      {\n" +
            "        \"query_path\": {\n" +
            "          \"path\": \"{{controllerPath}}/saveOrUpdate\",\n" +
            "          \"params\": []\n" +
            "        },\n" +
            "        \"edit_uid\": 0,\n" +
            "        \"status\": \"undone\",\n" +
            "        \"type\": \"static\",\n" +
            "        \"req_body_is_json_schema\": true,\n" +
            "        \"res_body_is_json_schema\": true,\n" +
            "        \"api_opened\": false,\n" +
            "        \"index\": 0,\n" +
            "        \"tag\": [],\n" +
            "        \"_id\": 850,\n" +
            "        \"method\": \"PUT\",\n" +
            "        \"catid\": 238,\n" +
            "        \"title\": \"添加or修改\",\n" +
            "        \"path\": \"{{controllerPath}}/saveOrUpdate\",\n" +
            "        \"project_id\": 48,\n" +
            "        \"req_params\": [],\n" +
            "        \"res_body_type\": \"json\",\n" +
            "        \"uid\": 15,\n" +
            "        \"add_time\": 1605254695,\n" +
            "        \"up_time\": 1605255458,\n" +
            "        \"req_query\": [],\n" +
            "        \"req_headers\": [\n" +
            "          {\n" +
            "            \"required\": \"1\",\n" +
            "            \"_id\": \"5fae41225d76008d2ddfde5c\",\n" +
            "            \"name\": \"Content-Type\",\n" +
            "            \"value\": \"application/json\"\n" +
            "          }\n" +
            "        ],\n" +
            "        \"req_body_form\": [],\n" +
            "        \"__v\": 0,\n" +
            "        \"desc\": \"<p>有id是修改<br>\\n无id则添加</p>\\n\",\n" +
            "        \"markdown\": \"有id是修改\\n无id则添加\",\n" +
            "        \"req_body_other\": \"\",\n" +
            "        \"req_body_type\": \"json\",\n" +
            "        \"res_body\": \"\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"query_path\": {\n" +
            "          \"path\": \"{{controllerPath}}/list\",\n" +
            "          \"params\": []\n" +
            "        },\n" +
            "        \"edit_uid\": 0,\n" +
            "        \"status\": \"undone\",\n" +
            "        \"type\": \"static\",\n" +
            "        \"req_body_is_json_schema\": true,\n" +
            "        \"res_body_is_json_schema\": true,\n" +
            "        \"api_opened\": false,\n" +
            "        \"index\": 0,\n" +
            "        \"tag\": [],\n" +
            "        \"_id\": 865,\n" +
            "        \"method\": \"GET\",\n" +
            "        \"catid\": 238,\n" +
            "        \"title\": \"精确查询\",\n" +
            "        \"path\": \"{{controllerPath}}/list\",\n" +
            "        \"project_id\": 48,\n" +
            "        \"req_params\": [],\n" +
            "        \"res_body_type\": \"json\",\n" +
            "        \"uid\": 15,\n" +
            "        \"add_time\": 1605254745,\n" +
            "        \"up_time\": 1605255556,\n" +
            "        \"req_query\": {{req_query}},\n" +
            "        \"req_headers\": [],\n" +
            "        \"req_body_form\": [],\n" +
            "        \"__v\": 0,\n" +
            "        \"desc\": \"<p>无参数则查询所有</p>\\n\",\n" +
            "        \"markdown\": \"无参数则查询所有\",\n" +
            "        \"res_body\": \"\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"query_path\": {\n" +
            "          \"path\": \"{{controllerPath}}/delete/{id}\",\n" +
            "          \"params\": []\n" +
            "        },\n" +
            "        \"edit_uid\": 0,\n" +
            "        \"status\": \"undone\",\n" +
            "        \"type\": \"var\",\n" +
            "        \"req_body_is_json_schema\": true,\n" +
            "        \"res_body_is_json_schema\": true,\n" +
            "        \"api_opened\": false,\n" +
            "        \"index\": 0,\n" +
            "        \"tag\": [],\n" +
            "        \"_id\": 855,\n" +
            "        \"method\": \"DELETE\",\n" +
            "        \"catid\": 238,\n" +
            "        \"title\": \"通过id删除\",\n" +
            "        \"path\": \"{{controllerPath}}/delete/{id}\",\n" +
            "        \"project_id\": 48,\n" +
            "        \"req_params\": [\n" +
            "          {\n" +
            "            \"_id\": \"5fae3e625d76008d2ddfde36\",\n" +
            "            \"name\": \"id\",\n" +
            "            \"desc\": \"\"\n" +
            "          }\n" +
            "        ],\n" +
            "        \"res_body_type\": \"json\",\n" +
            "        \"uid\": 15,\n" +
            "        \"add_time\": 1605254714,\n" +
            "        \"up_time\": 1605254754,\n" +
            "        \"req_query\": [],\n" +
            "        \"req_headers\": [\n" +
            "          {\n" +
            "            \"required\": \"1\",\n" +
            "            \"_id\": \"5fae3e625d76008d2ddfde37\",\n" +
            "            \"name\": \"Content-Type\",\n" +
            "            \"value\": \"application/json\"\n" +
            "          }\n" +
            "        ],\n" +
            "        \"req_body_form\": [],\n" +
            "        \"__v\": 0,\n" +
            "        \"desc\": \"\",\n" +
            "        \"markdown\": \"\",\n" +
            "        \"req_body_other\": \"\",\n" +
            "        \"req_body_type\": \"json\",\n" +
            "        \"res_body\": \"\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"query_path\": {\n" +
            "          \"path\": \"{{controllerPath}}/get/{id}\",\n" +
            "          \"params\": []\n" +
            "        },\n" +
            "        \"edit_uid\": 0,\n" +
            "        \"status\": \"undone\",\n" +
            "        \"type\": \"var\",\n" +
            "        \"req_body_is_json_schema\": false,\n" +
            "        \"res_body_is_json_schema\": false,\n" +
            "        \"api_opened\": false,\n" +
            "        \"index\": 0,\n" +
            "        \"tag\": [],\n" +
            "        \"_id\": 860,\n" +
            "        \"method\": \"GET\",\n" +
            "        \"catid\": 238,\n" +
            "        \"title\": \"通过id获取\",\n" +
            "        \"path\": \"{{controllerPath}}/get/{id}\",\n" +
            "        \"project_id\": 48,\n" +
            "        \"req_params\": [\n" +
            "          {\n" +
            "            \"_id\": \"5fae3e495d76008d2ddfde34\",\n" +
            "            \"name\": \"id\",\n" +
            "            \"desc\": \"\"\n" +
            "          }\n" +
            "        ],\n" +
            "        \"res_body_type\": \"json\",\n" +
            "        \"uid\": 15,\n" +
            "        \"add_time\": 1605254729,\n" +
            "        \"up_time\": 1605254729,\n" +
            "        \"req_query\": [],\n" +
            "        \"req_headers\": [],\n" +
            "        \"req_body_form\": [],\n" +
            "        \"__v\": 0\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "]";
}
